package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.BindingInvitationContract;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/13
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class BindingInvitationPresenter {
    public Activity activity;
    public AppCompatActivity me;
    public BindingInvitationContract contract;

    public BindingInvitationPresenter(Activity activity, AppCompatActivity me) {
        this.activity = activity;
        this.me = me;
    }

    public BindingInvitationPresenter(Activity activity, AppCompatActivity me, BindingInvitationContract contract) {
        this.activity = activity;
        this.me = me;
        this.contract = contract;
    }

    public void getWhoInviteMe(String token, String invite_code, int type) {
        if (type == 2) {
            if (TextUtils.isEmpty(invite_code)) {
                ToastUtil.show("请输入邀请码！");
                return;
            }
        }

        Model.getInstance().getUserApi().getWhoInviteMe(token, invite_code)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }

                        if (map.get("message").equals("绑定成功")) {
                            activity.finish();
                            return;
                        }
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));


                        if (data == null) {
                            if (type == 2) {
                                ToastUtil.show(map.get("message"));
                            }
                            return;
                        }
                        if (type == 1) {
                            contract.success(data);
                        } else {
                            activity.finish();
                        }
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {
                    }
                });
    }
}